#include "table.h"
#include "db.h"
#include <iostream>
#include "output.h"
#include <set>

//打印表格
void printf_table(int index)
{
    std::cout << "----------------------------------------" << std::endl;
    std::cout << "ID ";
    for (int i = 0; i < all_table[index].col_num; i++)
    {
        std::cout << all_table[index].column[i] + '('+ all_table[index].type[i] + ')' << " ";
    }
    std::cout << std::endl;
    std::cout << "----------------------------------------" << std::endl;
    for (int i = 0; i < all_table[index].row_num; i++)
    {
        std::cout << i + 1 << " ";
        for (int j = 0; j < all_table[index].col_num; j++)
            std::cout << table_data[i][j] << " ";
        std::cout << std::endl;
        std::cout << "----------------------------------------" << std::endl;
    }
}
void printf_col(int index,int col_chosen)
{
    std::cout << "----------------------------------------" << std::endl;
    std::cout << "ID ";
    
    std::cout << all_table[index].column[col_chosen] + '('+ all_table[index].type[col_chosen] + ')' << " ";
    
    std::cout << std::endl;
    std::cout << "----------------------------------------" << std::endl;
    for (int i = 0; i < all_table[index].row_num; i++)
    {
        std::cout << i + 1 << " ";
            std::cout << table_data[i][col_chosen] << " ";
        std::cout << std::endl;
        std::cout << "----------------------------------------" << std::endl;
    }
}
 
//打印所有表格列表
void printf_table_list()
{
    for (int i = 0; i < table_num; i++)
        std::cout << my_db[i].table_name  << std::endl;
}

//打印SELECT中的where后产生结果的表格
void printf_select(int index,std::vector<int> datas){
    std::set<int> prints;
    for(int i = 0;i < (int)datas.size();i++){
        prints.insert(datas[i]);
    }
    std::cout << "----------------------------------------" << std::endl;
    std::cout << "ID ";
    for (int i = 0; i < all_table[index].col_num; i++)
    {
        std::cout << all_table[index].column[i] + '('+ all_table[index].type[i] + ')' << " ";
    }
    std::cout << std::endl;
    std::cout << "----------------------------------------" << std::endl;
    for (int i = 0; i < all_table[index].row_num; i++)
    {
        if(prints.find(i) != prints.end()){
            std::cout << i + 1 << " ";
        for (int j = 0; j < all_table[index].col_num; j++)
            std::cout << table_data[i][j] << " ";
        std::cout << std::endl;
        std::cout << "----------------------------------------" << std::endl;
        }
    }
}

void printf_select_col(int index,std::vector<int> datas,int col_chosen){
    std::set<int> prints;
    for(int i = 0;i < (int)datas.size();i++){
        prints.insert(datas[i]);
    }
    std::cout << "----------------------------------------" << std::endl;
    std::cout << "ID ";
    
        std::cout << all_table[index].column[col_chosen] + '('+ all_table[index].type[col_chosen] + ')' << " ";

    std::cout << std::endl;
    std::cout << "----------------------------------------" << std::endl;
    for (int i = 0; i < all_table[index].row_num; i++)
    {
        if(prints.find(i) != prints.end()){
            std::cout << i + 1 << " ";
            std::cout << table_data[i][col_chosen] << " ";
        std::cout << std::endl;
        std::cout << "----------------------------------------" << std::endl;
        }
    }
}
